/**
 * watering task page.
 */

function deleteTask(taskId) {
	$.ajax({
		url : ('/_deleteTask?taskId=' + taskId),
		type : 'POST',
		success : function(result) {
			if (result != 'ok') {
				showMsgPopup('load task info failed:' + result.err,
						"#taskMgr");
			} else {
				location.reload();
			}
		},
		error : function(_, status, e) {
			showMsgPopup('delete task failed:' + e, "#taskMgr");
		}
	});
}

function deleteGroups(ids, type, taskId) {
	$.ajax({
		url : ('/_deleteTaskGroups'),
		type : 'POST',
		data : {groupIds:ids, 'type':type, 'taskId':taskId},
		success : function(result) {
			if (result != 'ok') {
				showMsgPopup('delete groups info failed:' + result.err,
						"#taskMgr");
			} else {
				$('#taskViewContainer').load('/loadTask?taskId='+taskId, function(){
					$('#taskViewContainer').trigger('create');
				});				
			}
		},
		error : function(_, status, e) {
			showMsgPopup('delete group failed:' + e, "#taskMgr");
		}
	});
}

function rightPageInit() {
	$('#editTask').off('click').on('click', function(event) {
		$.mobile.changePage("/addTask?taskId=" + __taskId, {
			transition : "flip",
			role : "dialog"
		});
		event.preventDefault();
	});

	$('#deleteTask').off('click').on('click', function(event) {
		var id = $("#textinput-id").val();
		if (!id || id.length < 1) {
			showMsgPopup("Please select a task to delete");
			return;
		}
		var xdata = {
			title : 'Confirm delete task',
			hint : "Are you sure?",
			msg : "Delete the task with id:" + id
		};
		showConfirmDlg(xdata, function() {
			deleteTask(id);
		});
		event.preventDefault();
	});

	$('#addPumpGroup').off('click').on('click', function(event) {
		$.mobile.changePage("/addTaskGroup?type=pump&taskId=" + __taskId, {
			transition : "flip",
			role : "dialog"
		});
		event.preventDefault();
	});

	$("#selectAllPumpGroup").off('click').on('click', function(event) {
		$("#lvPumpGroups").find("li").each(function(i, a) {
			var li = $(this);
			var checkBox = li.find("input");
			if (checkBox) {
				checkBox.prop('checked', true).checkboxradio('refresh');
			}
		});
	});

	$("#deSelectAllPumpGroup").off('click').on('click', function(event) {
		$("#lvPumpGroups").find("li").each(function(i, a) {
			var li = $(this);
			var checkBox = li.find("input");
			if (checkBox) {
				checkBox.prop('checked', false).checkboxradio('refresh');
			}
		});
	});
	
	$('#deletePumpGroups').off('click').on('click', function(event) {
		var taskId = $('#textinput-id').val();
        var ids = [];
        var msgStr='';
        
        $("#lvPumpGroups").find("li").each(function() {
              var li = $(this);
              var checkBox = li.find("input");
              if (checkBox && checkBox.is(':checked')) {
                  var id = checkBox.parent().parent().attr('id');
                  var name = checkBox.parent().text();
                  ids.push(id);
                  msgStr +=  name;
              }
          });
        
        if (ids.length == 0) {
            showMsgPopup("no group to be delete!");
        } else {
            var xdata = {title:'Confirm delete groups',hint:"Are you sure to delete groups:", msg:msgStr};
            showConfirmDlg(xdata, function(){
                deleteGroups(ids, "pump", taskId);
            });
        }
	});
	
	
	$('#addValveGroup').off('click').on('click', function(event) {
		$.mobile.changePage("/addTaskGroup?type=valve&taskId=" + __taskId, {
			transition : "flip",
			role : "dialog"
		});
		event.preventDefault();
	});

	$("#selectAllValveGroup").off('click').on('click', function(event) {
		$("#lvValveGroups").find("li").each(function() {
			var li = $(this);
			var checkBox = li.find("input");
			if (checkBox) {
				checkBox.prop('checked', true).checkboxradio('refresh');
			}
		});
	});

	$("#deSelectAllValveGroup").off('click').on('click', function(event) {
		$("#lvValveGroups").find("li").each(function(i, a) {
			var li = $(this);
			var checkBox = li.find("input");
			if (checkBox) {
				checkBox.prop('checked', false).checkboxradio('refresh');
			}
		});
	});
	
	$('#deleteValveGroups').off('click').on('click', function(event) {
		var taskId = $('#textinput-id').val();
        var ids = [];
        var msgStr='';
        
        $("#lvValveGroups").find("li").each(function() {
              var li = $(this);
              var checkBox = li.find("input");
              if (checkBox && checkBox.is(':checked')) {
                  var id = checkBox.parent().parent().attr('id');
                  var name = checkBox.parent().text();
                  ids.push(id);
                  msgStr +=  name;
              }
          });
        
        if (ids.length == 0) {
            showMsgPopup("no group to be delete!");
        } else {
            var xdata = {title:'Confirm delete groups',hint:"Are you sure to delete groups:", msg:msgStr};
            showConfirmDlg(xdata, function(){
            	deleteGroups(ids, "valve", taskId);
            });
        }
	});
}

rightPageInit();
